# -*- coding: utf-8 -*-
from odoo import models, fields,api, _

class HRLeaveSheet1(models.Model):
    _name = 'hr.leave.sheet'
    _order = 'id desc'
    _approve_by_level = True

    _inherit = ['portal.mixin', 'mail.thread', 'mail.activity.mixin', 'utm.mixin', 'base.approve']

    state = fields.Selection([
        ('draft', u'草稿'),
        ('cancel', u'取消'),
        ('to approve', u'待审核'),
        ('confirm', u'拒绝'),
        ('rejected', u'拒绝'),
        ('done', u'完成'),
    ], string=u"状态", default='draft', track_visibility='onchange')

    def get_sheet_level(self):
        return 1

    def _default_employee(self):
        return self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1)
    @api.onchange('employee_id')
    def onchange_employee_id(self):
        if self.employee_id:
            self.department_id=self.employee_id.department_id.id

    line_ids=fields.One2many('hr.leave','sheet_id')

    employee_id = fields.Many2one('hr.employee', default=_default_employee)
    department_id = fields.Many2one('hr.department', string=u'部门')
    # apply_date = fields.Date(string=u'日期')
    name = fields.Char()
    user_id = fields.Many2one('res.users', default=lambda self: self.env.uid)
    approve_flow_ids = fields.One2many('approve.flow', 'res_id', domain=[('res_model', '=', 'hr.leave.sheet')])

    approval_record_ids = fields.One2many('approval.records', 'res_id',
                                          domain=[('res_model', '=', 'hr.leave.sheet')])




    @api.model
    def create(self, vals):
        if not vals.get('name') or vals['name'] == _('New'):
            vals['name'] = self.env['ir.sequence'].next_by_code('hr.leave.sheet') or _('New')
        return super(HRLeaveSheet1, self).create(vals)


    def set_to_done(self):
        super(HRLeaveSheet1, self).set_to_done()
        for line in self.line_ids:
            line.create_line_ids()

